$(document).ready(function () {
        $("#doc-modal-1").modal({ width: '600px' });
        //$("#btnChangeHeader").click(function(){
        //		  $("#doc-modal-1").modal({width:'600px'});
        //});
});
$(function() {
    'use strict';
    // 初始化
    var $image = $('#image');
    $image.cropper({
        aspectRatio: 1,
        autoCropArea: 0.8,
        autoCrop:true,
        preview: '.up-pre-after'
    });

    // 事件代理绑定事件
    $('.docs-buttons').on('click', '[data-method]', function() { 
        var $this = $(this);
        var data = $this.data();
        var result = $image.cropper(data.method, data.option, data.secondOption);
        switch (data.method) {
            case 'getCroppedCanvas':
            if (result) {
                // 显示 Modal
                $('#cropped-modal').modal().find('.am-modal-bd').html(result);
                $('#download').attr('href', result.toDataURL('image/jpeg'));
            }
            break;
        }
    });

    // 上传图片
    var $inputImage = $('#inputImage');
    var URL = window.URL || window.webkitURL;
    var blobURL;

    if (URL) {
        $inputImage.change(function () {
            var files = this.files;
            var file;

            if (files && files.length) {
               file = files[0];

               if (/^image\/\w+$/.test(file.type)) {
                    blobURL = URL.createObjectURL(file);
                    $image.one('built.cropper', function () {
                        // Revoke when load complete
                       URL.revokeObjectURL(blobURL);
                    }).cropper('reset').cropper('replace', blobURL);
                    $inputImage.val('');
                } else {
                    window.alert('Please choose an image file.');
                }
            }

            // Amazi UI 上传文件显示代码
            var fileNames = '';
            $.each(this.files, function() {
                fileNames += '<span class="am-badge">' + this.name + '</span> ';
            });
            $('#file-list').html(fileNames);
        });
    } else {
        $inputImage.prop('disabled', true).parent().addClass('disabled');
    }
    //绑定上传事件
    $('#up-btn-ok').on('click', function () {
    	var $modal = $('#my-modal-loading');
    	var $modal_alert = $('#my-alert');
    	var img_src = $image.attr("src");
    	if (img_src === "") {
    		set_alert_info("没有选择上传的图片");
    		$modal_alert.modal();
    		return false;
    	}
    	$modal.modal();
    	var url = $.ctx() + $(this).attr("url"),
        picType = $(this).attr("picType");
    	var canvas = $("#image").cropper('getCroppedCanvas');
        //var data=canvas.toDataURL(); //转成base64
    	var data;
    	compressPic(canvas.toDataURL(), 400, function (str) {
    	    data = str;
    	    $.ajax({
    	        url: url,
    	        dataType: 'json',
    	        type: "POST",
    	        data: { "image": data.toString() },
    	        success: function (data, textStatus) {
    	            if (data.Statu === 0) {
                        //logo上传
    	                if (picType === "logo") 
    	                    $('#ownHead', parent.document).attr("src", data.Data.toString().replace("~", $.ctx()));
    	                else {
    	                    $('#ownHead', parent.document).attr("src", data.Data.toString().replace("~", $.ctx()));
    	                    $(".dropdown-toggle", parent.document).trigger("click");
    	                    $(".active", parent.document).children("i").trigger("click");
    	                }
    	            }
    	        },
    	        error: function () {
    	            $modal.modal('close');
    	            set_alert_info("上传文件失败了！");
    	            $modal_alert.modal();
    	            //console.log('Upload error');  
    	        }
    	    });
    	});
    });
});

function rotateimgright() {
$("#image").cropper('rotate', 90);
}


function rotateimgleft() {
$("#image").cropper('rotate', -90);
}

function set_alert_info(content){
	$("#alert_content").html(content);
}
//js压缩base64位图片
function compressPic(base64, w, callback) {
    var newImage = new Image();
    var quality = 0.8;    //压缩系数0-1之间
    newImage.src = base64;
    newImage.setAttribute("crossOrigin", 'Anonymous');	//url为外域时需要
    var imgWidth, imgHeight;
    newImage.onload = function () {
        imgWidth = this.width;
        imgHeight = this.height;
        var canvas = document.createElement("canvas");
        var ctx = canvas.getContext("2d");
        if (Math.max(imgWidth, imgHeight) > w) {
            if (imgWidth > imgHeight) {
                canvas.width = w;
                canvas.height = w * imgHeight / imgWidth;
            } else {
                canvas.height = w;
                canvas.width = w * imgWidth / imgHeight;
            }
        } else {
            canvas.width = imgWidth;
            canvas.height = imgHeight;
            quality = 0.6;
        }
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        ctx.drawImage(this, 0, 0, canvas.width, canvas.height);
        var base64 = canvas.toDataURL("image/jpeg", quality); //压缩语句
        // 如想确保图片压缩到自己想要的尺寸,如要求在50-150kb之间，请加以下语句，quality初始值根据情况自定
        // while (base64.length / 1024 > 150) {
        // 	quality -= 0.01;
        // 	base64 = canvas.toDataURL("image/jpeg", quality);
        // }
        // 防止最后一次压缩低于最低尺寸，只要quality递减合理，无需考虑
        // while (base64.length / 1024 < 50) {
        // 	quality += 0.001;
        // 	base64 = canvas.toDataURL("image/jpeg", quality);
        // }
        callback(base64);//必须通过回调函数返回，否则无法及时拿到该值
    }
}